<template>
  <div class='login flex justify-center items-center h-full'>
    <div class="content w-[385px]">
      <h2 class="text-center mb-[18px]">后台管理系统</h2>
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules">
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" size="medium"> <i slot="prefix"
              class="el-input__icon el-icon-user"></i></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="ruleForm.password" type="password" size="medium"> <i slot="prefix"
              class="el-input__icon el-icon-lock"></i></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input v-model="ruleForm.code" size="medium" class="w-[63%]"> <i slot="prefix"
              class="el-input__icon el-icon-goods"></i></el-input>
          <div class="float-right w-[35%] ml-[2%] cursor-pointer" @click="updataCode" >
            <img :src="srcImg" alt="">
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="w-full" @click="submitForm('ruleForm')">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { authCode, authLogin } from '@/api/login.js'
import { encrypt } from '@/utils/rsaEncrypt.js'
import Cookies from 'js-cookie'
export default {
  name: '',
  props: [],
  components: {}, // 定义子组件
  data () {
    return {
      ruleForm: {
        username: '', // 用户名
        password: '', //  密码
        code: '', // 验证码
        uuid: '' //
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      },
      srcImg: ''
    }
  },
  computed: {}, // 计算
  watch: {}, // 监视
  methods: {
    updataCode () {
      authCode().then(res => {
        console.log(res)
        const { img, uuid } = res
        this.srcImg = img
        this.ruleForm.uuid = uuid
      })
    },
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.password = encrypt(this.ruleForm.password)
          authLogin(this.ruleForm).then(res => {
            const { token, user } = res
            Cookies.set('token', token)
            localStorage.setItem('user', JSON.stringify(user))
            this.$router.push('/')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {
    this.updataCode()
    // console.log(encrypt(123456))
  }
}
</script>
<style lang='scss' scoped>
/* scoped 使用得定义的样式只在本页面内生效  */
.login {
  background: url('~@/assets/images/background.jpg') no-repeat;
  background-size: cover;
}

.content {
  padding: 25px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
  background-color: #fff;
}</style>
